package com.ruicar.afs.cloud.apply.contract.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.apply.common.dto.ApplySigningInfoDTO;
import com.ruicar.afs.cloud.apply.common.dto.SigningCustInfoDTO;
import com.ruicar.afs.cloud.apply.common.feign.ApplySigningFeign;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.apply.contract.condition.ApplySignCondition;
import com.ruicar.afs.cloud.apply.contract.service.ApplyBankCardService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * 银行签约丶短信
 *
 * @author 耿继辉
 */
@RestController
@Slf4j
@Api("银行签约丶短信")
@RequestMapping("/Signing")
@AllArgsConstructor
public class ApplySigningController {
    private final ApplySigningFeign applySigningFeign;
    private final ApplyBankCardService applyBankCardService;

    /**
     * 四要素验证
     *
     * @return
     */
    @ApiOperation("四要素验证")
    @PostMapping("/doVerify")
    public IResponse doVerify(@RequestBody ApplySignCondition applySignCondition) {
        if(applySignCondition.getChlCd().equals(ApplyConstants.ABC)){
            applySignCondition.setChlCd(ApplyConstants.SIGN_ABC);
        }else if(applySignCondition.getChlCd().equals(ApplyConstants.ICBC)){
            applySignCondition.setChlCd(ApplyConstants.SIGN_ICBC);
        }else {
            applySignCondition.setChlCd(ApplyConstants.SIGN_CCB);
        }
        ApplySigningInfoDTO applySigningInfoDTO = new ApplySigningInfoDTO();
        applySigningInfoDTO.setChlCd(applySignCondition.getChlCd());
        applySigningInfoDTO.setMsgid(UUID.randomUUID().toString().replace("-",""));
        SigningCustInfoDTO signingCustInfoDTO = new SigningCustInfoDTO();
        signingCustInfoDTO.setChlCd(applySignCondition.getChlCd());
        signingCustInfoDTO.setTel(applySignCondition.getTel());
        signingCustInfoDTO.setPayAcctNo(applySignCondition.getPayAcctNo());
        signingCustInfoDTO.setPayAcctNm(applySignCondition.getPayAcctNm());
        signingCustInfoDTO.setCardNo(applySignCondition.getCardNo());
        signingCustInfoDTO.setCardType("01");
        signingCustInfoDTO.setPayType("0001");
        applySigningInfoDTO.setBody(signingCustInfoDTO);
        JSONObject jsonObject=(JSONObject) JSONObject.toJSON(applySigningInfoDTO);
        log.info(jsonObject.toString());
        applyBankCardService.custSigningApply(applySigningInfoDTO);
        return IResponse.success("验证成功");
    }

    /**
     * @return
     */
    @ApiOperation("短信验证")
    @PostMapping("/doSign")
    public IResponse doSign(@RequestBody ApplySignCondition applySignCondition) {
        if(applySignCondition.getChlCd().equals(ApplyConstants.ABC)){
            applySignCondition.setChlCd(ApplyConstants.SIGN_ABC);
        }else if(applySignCondition.getChlCd().equals(ApplyConstants.ICBC)){
            applySignCondition.setChlCd(ApplyConstants.SIGN_ICBC);
        }else {
            applySignCondition.setChlCd(ApplyConstants.SIGN_CCB);
        }
        ApplySigningInfoDTO applySigningInfoDTO = new ApplySigningInfoDTO();
        applySigningInfoDTO.setChlCd(applySignCondition.getChlCd());
        applySigningInfoDTO.setMsgid(UUID.randomUUID().toString().replace("-",""));
        SigningCustInfoDTO signingCustInfoDTO = new SigningCustInfoDTO();
        signingCustInfoDTO.setChlCd(applySignCondition.getChlCd());
        signingCustInfoDTO.setTel(applySignCondition.getTel());
        signingCustInfoDTO.setPayAcctNo(applySignCondition.getPayAcctNo());
        signingCustInfoDTO.setPayAcctNm(applySignCondition.getPayAcctNm());
        signingCustInfoDTO.setCardNo(applySignCondition.getCardNo());
        signingCustInfoDTO.setCardType("01");
        signingCustInfoDTO.setPayType("0001");
        signingCustInfoDTO.setVc(applySignCondition.getVc());
        applySigningInfoDTO.setBody(signingCustInfoDTO);
        JSONObject jsonObject=(JSONObject) JSONObject.toJSON(applySigningInfoDTO);
        log.info(jsonObject.toString());
        this.applySigningFeign.doSign(applySigningInfoDTO);
        return IResponse.success("验证成功");
    }
}
